tinyMCEPopup.requireLangPack();

var StyleLibraryOverviewDialog = {
	init : function() {
		var f = document.forms[0];
		var ed = tinyMCEPopup.editor;
		
		if(ed.settings.user_css)
			ed.plugins.stylelibrary.reloadCssFile(ed, ed.settings.user_css);

		if(ed.settings.stylelibrary_headhtml) {
			var head = document.getElementsByTagName("head")[0];
			jQuery(head).append(ed.settings.stylelibrary_headhtml);
		}
	},
	
	useStyle : function(styleType, className) {
		tinyMCEPopup.restoreSelection();
		var ed = tinyMCEPopup.editor;
		var elm = ed.dom.getParent(ed.selection.getNode(), 'div,table,img');
		
		if(!elm || styleType != elm.nodeName.toLowerCase()) {
			alert(tinyMCEPopup.getLang("stylelibrary_dlg.not_applicable_" + styleType));
		} else {
			// FIXME: Check does not work in IE, because it can't find user_css after its refresh
			//if(elm && ed.settings['user_css']) {
			
				// Load styles available for the active node's type
			//	var u = ed.documentBaseURI.toAbsolute(ed.settings['user_css']);
			//	var cl = ed.dom.getClasses(u, elm.nodeName.toLowerCase());

				// Avoid adding "img" class to "table"
			//	for(var i = 0; i < cl.length; i++) {
			//		if(className == cl[i]['class']) {
						elm.className = className;
			//			break;
			//		}
			//	}
			//}
			tinyMCEPopup.close();
		}
	}
};

tinyMCEPopup.onInit.add(StyleLibraryOverviewDialog.init, StyleLibraryOverviewDialog);
